package logic

import (
	"context"
	"encoding/json"
	"go-zero-example/cmd/userrpc/internal/dao"
	"go-zero-example/cmd/userrpc/internal/model"
	"go-zero-example/cmd/userrpc/internal/svc"
	"go-zero-example/cmd/userrpc/internal/util"
	"go-zero-example/cmd/userrpc/pb/userrpc"
	"strconv"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserLogic {
	return &GetUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetUser 获取用户
func (l *GetUserLogic) GetUser(in *userrpc.GetUserRequest) (*userrpc.GetUserResponse, error) {
	// todo: add your logic here and delete this line
	res := new(userrpc.GetUserResponse)
	user := new(model.User)
	user.Id = util.ConvertToInternalID(strconv.FormatInt(in.Userid, 10))
	userDao := new(dao.UserDao)
	err := userDao.GetUserByUid(user)
	userJSON, err := json.Marshal(user)
	res.User = string(userJSON)
	if err != nil {
		res.Message = "获取失败"
		return nil, err
	}
	if err != nil {

		res.Message = "获取失败"
		return nil, err
	}
	res.Message = "获取成功"
	return res, nil
}
